﻿using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraTab;
using EkStudio.com.bean.xml.BillModule;
using EkStudio.com.bean.xml.reflection.JAXB;
using EkStudio.com.bean.xml.RootInterface.Field;
using EkStudio.com.bean.xml.RootInterface.FieldEvent;
using EkStudio.com.bean.xml.RootInterface.Plugin;
using EkStudio.com.bean.xml.Unit;
using EkStudio.com.common.clipboard;
using EkStudio.com.common.unit;
using EkStudio.com.ui.form;
using EkStudio.com.ui.unitbill.property;
using EkStudio.com.ui.unitbill.property.Event;
using EkStudio.Com.UI.Interface;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml;

namespace EkStudio.com.ui.unitbill
{
    public partial class BillModuleControl : XtraUserControl,ProjectUnitBillInterface
    {

        private log4net.ILog logger = log4net.LogManager.GetLogger(typeof(BillModuleControl));

        private bool readOnly = false;
        private bool save = true;
        private ProjectRootPanel RootPanel = null;
        private ProjectUnitBillInterface ParentUnit = null;
        private List<ProjectUnitBillInterface> SubjectUnit = new List<ProjectUnitBillInterface>();//子面板
        private BillUiModuleToProperty ModuleProperty = null;//模块主属性属性编辑器专用
        private BillModuleItem Module = null;

        private BillUiGroupItems SelectGroup = null;//当前选中并显示字段的分组

        //属性面板
        private ModuleUiGroupProperty GroupProperty = null;
        private ModuleUiFieldProperty FieldProperty = null;
        private ModuleUiEventProperty EventProperty = null;
        private ModuleUiJScriptProperty JScriptPropery = null;
        private ModuleUiJavaProperty JavaProperty = null;

        private bool ReLoadMenu = false;

        private bool moveing = false;

        private ModuleQueryUiForm QueryForm = null;
        private ModuleIconSelectForm IconForm = null;

        private System.Drawing.Color DefaultColor = System.Drawing.Color.Empty;

        //private ListViewItem[] oldSelect = null;



        public BillModuleControl(BillModuleItem _bean, ProjectRootPanel Root,bool _readOnly)
        {
            InitializeComponent();
            Module = _bean;
            RootPanel = Root;
            readOnly = _readOnly;
            InitBillModule();
            
        }
        private void InitBillModule()
        {
            ModuleProperty = new BillUiModuleToProperty(Module);

            //初始目录结构
            if(RootPanel.MenuItem.Business!=null && RootPanel.MenuItem.Business.Modules!=null
                && RootPanel.MenuItem.Business.Modules.Count > 0)
            {
                RepositoryMenuList.Items.Clear();
                for (int i=0; i < RootPanel.MenuItem.Business.Modules.Count; i++)
                {
                    CheckedListBoxItem item = new CheckedListBoxItem(RootPanel.MenuItem.Business.Modules[i].Caption, RootPanel.MenuItem.Business.Modules[i].Caption);
                    RepositoryMenuList.Items.Add(item);
                }
            }
            //初始字段分组
            ShowFieldGroup();
            ShowFieldEvent();
            ShowJScript();
            ShowJava();


            //属性面板
            GroupProperty = new ModuleUiGroupProperty(Module,RootPanel,this);
            GroupProperty.Dock = DockStyle.None;
            GroupProperty.Visible = false;
            GroupProperty.CaptionChanged += GroupCaptionChanged;

            FieldProperty = new ModuleUiFieldProperty(Module,RootPanel,this);
            FieldProperty.Dock = DockStyle.None;
            FieldProperty.Visible = false;
            FieldProperty.CaptionChanged += FieldCaptionChanged;
            FieldProperty.LinkerChange += LinkerChanged;

            EventProperty = new ModuleUiEventProperty(Module, RootPanel, this);
            EventProperty.Dock = DockStyle.None;
            EventProperty.Visible = false;
            EventProperty.CaptionChanged += EventCaptionChanged;

            JScriptPropery = new ModuleUiJScriptProperty(Module, RootPanel, this);
            JScriptPropery.Dock = DockStyle.None;
            JScriptPropery.Visible = false;
            JScriptPropery.CaptionChanged += JsCaptionChanged;

            JavaProperty = new ModuleUiJavaProperty(Module, RootPanel, this);
            JavaProperty.Dock = DockStyle.None;
            JavaProperty.Visible = false;
            JavaProperty.CaptionChanged += JavaCaptionChanged;

            FieldPropertyPage.Controls.Add(GroupProperty);
            FieldPropertyPage.Controls.Add(FieldProperty);
            FieldPropertyPage.Controls.Add(EventProperty);
            FieldPropertyPage.Controls.Add(JScriptPropery);
            FieldPropertyPage.Controls.Add(JavaProperty);

            //注册模块属性
            ModulePropertyList.SelectedObject = ModuleProperty;
            //初始化属性是否可编辑
            ModProperty_Field_2_MenuList.Properties.ReadOnly = ReadOnly;
            ModProperty_Field_3_Archive.Properties.ReadOnly = ReadOnly;
            ModProperty_Field_4_Attachment.Properties.ReadOnly = ReadOnly; 
            ModProperty_Field_5_Catalogue.Properties.ReadOnly = ReadOnly;
            ModProperty_Field_6_Reminder.Properties.ReadOnly = ReadOnly; 
            ModProperty_Field_7_Tags.Properties.ReadOnly = ReadOnly; 
            ModProperty_Field_8_Icon.Properties.ReadOnly = ReadOnly;
            ModProperty_Field_13_DataSource.Properties.ReadOnly = ReadOnly;
            ModProperty_Field_14_Compress.Properties.ReadOnly = ReadOnly;
            ModProperty_Field_16_DyEnable.Properties.ReadOnly = ReadOnly;
            ModProperty_Field_17_DyMsgType.Properties.ReadOnly = ReadOnly;
            ModProperty_Field_18_DyMsgTitle.Properties.ReadOnly = ReadOnly;
            ModProperty_Field_19_DyMsgBody.Properties.ReadOnly = ReadOnly;
            ModProperty_Field_20_DyMsgUserType.Properties.ReadOnly = ReadOnly;
            ModProperty_Field_21_DyMsgUser.Properties.ReadOnly = ReadOnly;

            RepositoryDyMsgTypeEdit.Items.AddRange(BeanUtil.DY_MESSAGE_TYPE_LIST);
            RepositoryDyMsgUserTypeEdit.Items.AddRange(BeanUtil.DY_MESSAGE_USER_TYPE_LIST);
        }

        //初始化字段分组
        private void ShowFieldGroup()
        {
            if (Module.UiField == null)
            {
                Module.UiField = new BillUiGroupRoot();
            }
            if (Module.UiField.FieldsProperties == null) Module.UiField.FieldsProperties = new List<BillUiGroupItems>();
            FieldsListView.Items.Clear();
            if (Module.UiField.FieldsProperties.Count > 0)
            {
                //ListViewItem("主要字段", 0);
                for (int i = 0; i < Module.UiField.FieldsProperties.Count; i++)
                {
                    BillUiGroupItems group = Module.UiField.FieldsProperties[i];
                    ListViewItem ViewIt = new ListViewItem(group.Caption, 0);
                    ViewIt.Tag = group;
                    FieldsListView.Items.Add(ViewIt);
                }
            }
            if (SelectGroup != null)
            {
                SelectGroup = null;
                HideFieldProperty();
            }
            
        }
        //打开字段集
        private void ShowFieldByGroup(BillUiGroupItems group)
        {
            FieldsListView.Items.Clear();
            if (group.Fields == null) { group.Fields = new List<BillUiFieldsItem>(); }
            if (group.Fields.Count > 0)
            {
                for(int i = 0; i < group.Fields.Count; i++)
                {
                    BillUiFieldsItem field = group.Fields[i];
                    ListViewItem ViewIt = new ListViewItem(field.Caption, 1);
                    ViewIt.Tag = field;
                    FieldsListView.Items.Add(ViewIt);
                }
            }
            GroupProperty.Visible = false;
            GroupProperty.Dock = DockStyle.None;

        }
        //加载字段事件
        private void ShowFieldEvent()
        {
            EventListView.Items.Clear();
            if (Module.DataFill == null)
            {
                Module.DataFill = new BillDataFillRoot();
                Module.DataFill.LinkEvents = new List<BillDataFillItem>();
            }
            if(Module.DataFill.LinkEvents == null)
            {
                Module.DataFill.LinkEvents = new List<BillDataFillItem>();
            }
            if (Module.DataFill.LinkEvents.Count > 0)
            {
                for(int i = 0; i < Module.DataFill.LinkEvents.Count; i++)
                {
                    BillDataFillItem it = Module.DataFill.LinkEvents[i];
                    ListViewItem ViewIt = new ListViewItem(it.Caption, 5);
                    ViewIt.Tag = it;
                    EventListView.Items.Add(ViewIt);
                }
            }
        }
        /// <summary>
        /// 加载JS脚本
        /// </summary>
        private void ShowJScript()
        {
            ScriptListView.Items.Clear();
            if(Module.ExpandCode == null)
            {
                Module.ExpandCode = new BillExpandCodeRoot();
            }
            if(Module.ExpandCode.Jscript == null)
            {
                Module.ExpandCode.Jscript = new BillExpandJsCodeRoot();
            }
            if(Module.ExpandCode.Jscript.Item == null)
            {
                Module.ExpandCode.Jscript.Item = new List<BillExpandJsCodeItem>();
            }

            if (Module.ExpandCode.Jscript.Item.Count > 0)
            {
                for(int i = 0; i < Module.ExpandCode.Jscript.Item.Count; i++)
                {
                    BillExpandJsCodeItem JItem = Module.ExpandCode.Jscript.Item[i];
                    ListViewItem ViewIt = new ListViewItem(JItem.Caption, 2);
                    ViewIt.Tag = JItem;
                    ScriptListView.Items.Add(ViewIt);
                }
            }
        }

        private void ShowJava()
        {
            JavaListView.Clear();
            if (Module.ExpandCode == null)
            {
                Module.ExpandCode = new BillExpandCodeRoot();
            }
            if(Module.ExpandCode.JavaPlugin == null)
            {
                Module.ExpandCode.JavaPlugin = new BillExpandJavaCodeRoot();
            }
            if(Module.ExpandCode.JavaPlugin.JavaItem == null)
            {
                Module.ExpandCode.JavaPlugin.JavaItem = new List<BillExpandJavaCodeItem>();
            }
            if (Module.ExpandCode.JavaPlugin.JavaItem.Count > 0)
            {
                for(int i = 0; i < Module.ExpandCode.JavaPlugin.JavaItem.Count; i++)
                {
                    BillExpandJavaCodeItem it = Module.ExpandCode.JavaPlugin.JavaItem[i];
                    ListViewItem ViewIt = new ListViewItem(it.Caption, 3);
                    ViewIt.Tag = it;
                    JavaListView.Items.Add(ViewIt);
                }
            }
        }

        public string BillKey
        {
            get { return Module.getKey(); }
        }


        public bool SaveStatus
        {
            get
            {
                return save;
            }

            set
            {
                save = value;
                RootPanel.SaveStatus = value;    
            }
        }
        /// <summary>
        /// 当前页面是否是子页面
        /// </summary>
        public bool isSubUnitBill
        {
            get
            {
                return false;
            }
        }
        
        public List<ProjectUnitBillInterface> SubjectPage
        {
            get
            {
                return SubjectUnit;
            }
        }

        /// <summary>
        /// 复制
        /// </summary>
        /// <param name="clazz">复制后的clazz</param>
        /// <returns></returns>
        public bool Copy(out object clazz)
        {
            bool val = false;
            BillModuleItem clone = Module.Clone() as BillModuleItem;
            clazz = clone;
            if (clazz != null) val = true;
            return val;
        }




        public bool ReadOnly
        {
            get
            {
                return readOnly;
            }

            set
            {
                readOnly = value;
            }
        }

        public ProjectUnitBillInterface ParentPage
        {
            get
            {
                return ParentUnit;
            }

            set
            {
                ParentUnit = value;
            }
        }

        public void AddSubjectBill(ProjectUnitBillInterface bill)
        {
            if (!SubjectUnit.Contains(bill))
            {
                bill.ParentPage = this;
                SubjectUnit.Add(bill);
            }
        }

    

        public bool close()
        {
            bool result = true;
            if (!save || !isSubSaveStatus())
            {
                DialogResult dialog =  XtraMessageBox.Show("当前数据已更改是否保存?", "提示", MessageBoxButtons.YesNoCancel);
                if (dialog == DialogResult.Yes)
                {
                    saveData(true);
                    result = true;
                }
                else if(dialog == DialogResult.No)
                {
                    result = true;
                }
                else
                {
                    result = false;
                }
            }

            if (result)
            {
                //释放模块占用
                try
                {
                    RootPanel.Http.ReleaseModule(Module.BillModule.Key, Module.BillModule.Caption, ProjectUtil.TYPE_BILL_MODULE);
                }catch(Exception e)
                {
                    RootPanel.AddConsoleText("页面关闭失败", true);
                    logger.Error("页面关闭失败", e);
                }
                
            }

            return result;
        }

        public void RemoveSubjectBill(ProjectUnitBillInterface bill)
        {
            if (SubjectUnit.Contains(bill))
            {
                bill.ParentPage = null;
                SubjectUnit.Remove(bill);
            }
        }
        /// <summary>
        /// 判断bll是否是当前页面的子页面
        /// </summary>
        /// <param name="bill"></param>
        /// <returns></returns>
        public bool ContainSubject(ProjectUnitBillInterface bill)
        {
            return SubjectUnit.Contains(bill);
        }

        /// <summary>
        /// 保存数据 参数为true 更新主界面工具栏状态
        /// </summary>
        /// <param name="updateParent"></param>
        public void saveData(bool updateParent)
        {
            if (!readOnly && (!save || !isSubSaveStatus())) 
            {
                if (!isSubSaveStatus())
                {
                    //先保存子表
                    foreach(ProjectUnitBillInterface subPage in SubjectUnit)
                    {
                        subPage.saveData(false);
                    }
                }

                Module.autoInitDataSource();
                Module.autoInitQueryCode();
                Module.autoInitLanguage(RootPanel.ProjectVersion);
                Module.BillModule.RootVersion = BeanUtil.VERSION;
                Module.BillModule.Owner.LastMender = RootPanel.UserInfo.UserName;
                Module.BillModule.Owner.LastModified = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                JAXBContext<BillModuleItem> jaxb = new JAXBContext<BillModuleItem>();
                
                string xml = jaxb.marshal(Module, !Module.BillModule.Compress);

                string result = RootPanel.Http.SaveXmlBean(Module.getKey(), ProjectUtil.TYPE_BILL_MODULE,xml);

                XmlDocument docXml = new XmlDocument();
                try
                {
                    docXml.LoadXml(result);
                    XmlElement root = docXml.DocumentElement;
                    XmlNode node = root.SelectSingleNode("result");
                    if (node.InnerText == "0")
                    {
                        RootPanel.AddConsoleText(string.Format("保存模块[{0}]成功", Module.getCaption()), false);
                    }
                    else
                    {
                        RootPanel.AddConsoleText(string.Format("保存模块[{0}]失败", Module.getCaption()), false);
                    }
                }
                catch(Exception e)
                {
                    RootPanel.AddConsoleText(string.Format("保存模块[{0}]失败", Module.getCaption()), false);
                    logger.Error("保存失败", e);
                }
                if (ReLoadMenu) RootPanel.InitMenu();
            }
            save = true;
            if(updateParent) RootPanel.SaveStatus = true;
            ModulePropertyList.Refresh();
        }
        /// <summary>
        /// 判定子页面保存状态
        /// </summary>
        /// <returns></returns>
        private bool isSubSaveStatus()
        {
            bool result = true;
            if (SubjectUnit.Count > 0)
            {
                for(int i = 0; i < SubjectUnit.Count; i++)
                {
                    if (!SubjectUnit[i].SaveStatus)
                    {
                        result = false;
                        break;
                    }
                }
            }

            return result;
        }
        //隐藏属性面板
        private void HideFieldProperty()
        {
            GroupProperty.Dock = DockStyle.None;
            FieldProperty.Dock = DockStyle.None;
            EventProperty.Dock = DockStyle.None;
            JScriptPropery.Dock = DockStyle.None;
            JavaProperty.Dock = DockStyle.None;
            GroupProperty.Visible = false;
            FieldProperty.Visible = false;
            EventProperty.Visible = false;
            JScriptPropery.Visible = false;
            JavaProperty.Visible = false;
        }


        #region 事件存放区域

        private void ModulePropertyList_DoubleClick(object sender, EventArgs e)
        {

        }

        private void FieldTabControl_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {
            if (FieldTabControl.SelectedTabPageIndex >= 0)
            {
                XtraTabPage page = FieldTabControl.SelectedTabPage;
                if(page.Controls.Count>0 && page.Controls[0] is ListView){
                    ListView view = (ListView)page.Controls[0];
                    view.ContextMenuStrip = FieldPopMenu;
                }
            }
        }
       
        /// <summary>
        /// 显示字段组或者字段属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FieldsListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (moveing) return;
            if (FieldsListView.SelectedItems != null && FieldsListView.SelectedItems.Count > 0)
            {
                if (DefaultColor == System.Drawing.Color.Empty) DefaultColor = FieldsListView.SelectedItems[0].BackColor;
                object item = FieldsListView.SelectedItems[0].Tag;
                if (item != null)
                {
                    PropertyTabControl.SelectedTabPageIndex = 0;
                    if (item is BillUiGroupItems)//当前是字段组
                    {
                        if (!GroupProperty.Visible)
                        {
                            HideFieldProperty();
                            GroupProperty.Dock = DockStyle.Fill;
                            GroupProperty.Visible = true;
                        }
                        BillUiGroupItems temp = (BillUiGroupItems)item;
                        if (FieldsListView.SelectedItems.Count == 1)
                        {

                            BillUiGroupToProperty group = new BillUiGroupToProperty(temp);
                            GroupProperty.SelectObject = group;
                        }
                        else
                        {

                            BillUiGroupToProperty[] groups = new BillUiGroupToProperty[FieldsListView.SelectedItems.Count];
                            for (int i = 0; i < FieldsListView.SelectedItems.Count; i++)
                            {
                                ListViewItem vIt = FieldsListView.SelectedItems[i];
                                object tag = vIt.Tag;
                                groups[i] = new BillUiGroupToProperty((BillUiGroupItems)tag);
                            }
                            GroupProperty.SelectObjects = groups;

                        }

                    }
                    else if (item is BillUiFieldsItem)//当前是字段
                    {
                        if (!FieldProperty.Visible)
                        {
                            HideFieldProperty();

                            FieldProperty.Dock = DockStyle.Fill;
                            FieldProperty.Visible = true;
                        }

                        BillUiFieldsItem temp = (BillUiFieldsItem)item;
                        if (FieldsListView.SelectedItems.Count == 1)
                        {
                            BillUiFieldToProperty group = new BillUiFieldToProperty(temp, SelectGroup);
                            FieldProperty.SelectField = group;
                            ListViewItem vIt = FieldsListView.SelectedItems[0];
                        }
                        else
                        {

                            BillUiFieldToProperty[] groups = new BillUiFieldToProperty[FieldsListView.SelectedItems.Count];
                            for (int i = 0; i < FieldsListView.SelectedItems.Count; i++)
                            {
                                ListViewItem vIt = FieldsListView.SelectedItems[i];
                                object tag = vIt.Tag;
                                groups[i] = new BillUiFieldToProperty((BillUiFieldsItem)tag, SelectGroup);
                            }
                            FieldProperty.SelectFields = groups;
                        }
                    }
                }
            }
        }

        private void EventListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (moveing) return;
            if (EventListView.SelectedItems != null && EventListView.SelectedItems.Count > 0)
            {
                PropertyTabControl.SelectedTabPageIndex = 0;
                int Len = EventListView.SelectedItems.Count;
                if (Len == 1)
                {
                    object tag = EventListView.SelectedItems[0].Tag;
                    if (tag is BillDataFillItem)
                    {
                        BillUiEventToProperty temp = new BillUiEventToProperty((BillDataFillItem)tag);
                        EventProperty.SelectObject = temp;
                    }
                }
                else
                {
                    BillUiEventToProperty[] temp = new BillUiEventToProperty[Len];
                    for (int i = 0; i < EventListView.SelectedItems.Count; i++)
                    {
                        object tag = EventListView.SelectedItems[i].Tag;
                        if (tag is BillDataFillItem)
                        {
                            temp[i] = new BillUiEventToProperty((BillDataFillItem)tag);
                        }
                    }
                    EventProperty.SelectObjects = temp;
                }

                if (!EventProperty.Visible)
                {
                    HideFieldProperty();

                    EventProperty.Dock = DockStyle.Fill;
                    EventProperty.Visible = true;
                }

            }
        }
        /// <summary>
        /// 扩展代码显示事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void JavaLiistView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (moveing) return;
            if (JavaListView.SelectedItems != null && JavaListView.SelectedItems.Count > 0)
            {
                PropertyTabControl.SelectedTabPageIndex = 0;
                int Len = JavaListView.SelectedItems.Count;
                if (Len == 1)
                {
                    if(JavaListView.SelectedItems[0].Tag is BillExpandJavaCodeItem)
                    {
                        BillExpandJavaCodeItem it = (BillExpandJavaCodeItem)JavaListView.SelectedItems[0].Tag;
                        BillUiJavaToProperty java = new BillUiJavaToProperty(it);
                        JavaProperty.SelectObject = java;
                        if (!JavaProperty.Visible)
                        {
                            HideFieldProperty();
                            JavaProperty.Dock = DockStyle.Fill;
                            JavaProperty.Visible = true;
                        }
                    }

                }
                else
                {
                    BillUiJavaToProperty[] javas = new BillUiJavaToProperty[Len];
                    for(int i = 0; i < Len; i++)
                    {
                        BillExpandJavaCodeItem it = (BillExpandJavaCodeItem)JavaListView.SelectedItems[i].Tag;
                        BillUiJavaToProperty java = new BillUiJavaToProperty(it);
                        javas[i] = java;
                    }
                    JavaProperty.SelectObjects = javas;
                    if (!JavaProperty.Visible)
                    {
                        HideFieldProperty();
                        JavaProperty.Dock = DockStyle.Fill;
                        JavaProperty.Visible = true;
                    }
                }
            }
        }



        private void FieldsListView_KeyDown(object sender, KeyEventArgs e)
        {
            
            if(e.KeyCode == Keys.A && e.Control)//全选
            {
                
                
            }else if(e.KeyCode == Keys.Back && SelectGroup!=null)
            {
                ShowFieldGroup();
            }
        }

        

        /// <summary>
        /// 字段组双击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FieldsListView_DoubleClick(object sender, EventArgs e)
        {
            if (FieldsListView.SelectedItems.Count == 1)
            {
                object temp = FieldsListView.SelectedItems[0].Tag;
                if(temp is BillUiGroupItems)
                {
                    SelectGroup = (BillUiGroupItems)temp;
                    ShowFieldByGroup(SelectGroup);
                }
            }
        }

        private void FieldTabControl_CustomHeaderButtonClick(object sender, DevExpress.XtraTab.ViewInfo.CustomHeaderButtonEventArgs e)
        {
            if (e.Button.Kind == ButtonPredefines.Left && SelectGroup != null)
            {
                ShowFieldGroup();
            }
        }


        #region 弹出目录注册事件

        /// <summary>
        /// 新建功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FieldPopNewNode_Click(object sender, EventArgs e)
        {
            if (readOnly) return;
            bool success = false;
            if (FieldTabControl.SelectedTabPage.Name == "FieldTabPage")
            {
                if (SelectGroup != null)//新建字段
                {
                    BillUiFieldsItem field = new BillUiFieldsItem();
                    field.DataProperty = new BillUiFieldsDataProperty();
                    field.EditBorder = new BillUiFieldsEditBorderProperty();
                    field.EditProperty = new BillUiFieldsEditProperty();
                    field.FormulaProperty = new BillUiFieldsFormulaProperty();
                    field.ViewBorder = new BillUiFieldsViewBorderProperty();
                    SelectGroup.addNode(field, Module);
                    ListViewItem it = new ListViewItem(field.Caption, 1);
                    it.Tag = field;
                    FieldsListView.Items.Add(it);
                    it.Selected = true;
                    success = true;
                }
                else //新建分组
                {
                    BillUiGroupItems group = new BillUiGroupItems();
                    group.Fields = new List<BillUiFieldsItem>();
                    Module.UiField.addNode(group,Module);
                    ListViewItem it = new ListViewItem(group.Caption, 0);
                    it.Tag = group;
                    FieldsListView.Items.Add(it);
                    it.Selected = true;
                    success = true;
                }
            }
            else if (FieldTabControl.SelectedTabPage.Name == "EventTabPage")//填充事件
            {
                BillDataFillItem EventIt = new BillDataFillItem();
                if (Module.DataFill == null) Module.DataFill = new BillDataFillRoot();
                if (Module.DataFill.LinkEvents == null) Module.DataFill.LinkEvents = new List<BillDataFillItem>();
                Module.DataFill.addNode(EventIt, Module);
                ListViewItem it = new ListViewItem(EventIt.Caption, 5);
                it.Tag = EventIt;
                EventListView.Items.Add(it);
                it.Selected = true;
                success = true;
            }
            else if (FieldTabControl.SelectedTabPage.Name == "ScriptTabPage")//脚本代码
            {
                BillExpandJsCodeItem js = new BillExpandJsCodeItem();
                
                Module.ExpandCode.Jscript.addNode(js,Module);
                ListViewItem it = new ListViewItem(js.Caption, 2);
                it.Tag = js;
                ScriptListView.Items.Add(it);
                it.Selected = true;
                success = true;
            }
            else if (FieldTabControl.SelectedTabPage.Name == "JavaTabPage")//扩展代码
            {
                BillExpandJavaCodeItem java = new BillExpandJavaCodeItem();
                java.JavaPackage = string.Format("com.kootop.plugin.{0}", Module.getJavaPackage());
                Module.ExpandCode.JavaPlugin.addNode(java,Module);
                ListViewItem it = new ListViewItem(java.Caption, 3);
                it.Tag = java;
                JavaListView.Items.Add(it);
                it.Selected = true;
                success = true;
            }
            if (success) SaveStatus = false;
        }

        /// <summary>
        /// 删除功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FieldPopDeleteNode_Click(object sender, EventArgs e)
        {
            if (readOnly) return;
            if (FieldTabControl.SelectedTabPage.Name == "FieldTabPage")
            {
                if (SelectGroup != null)//删除字段
                {
                    if(FieldsListView.SelectedItems.Count == 1)
                    {
                        DialogResult DR = XtraMessageBox.Show("您确认要删除当前选中字段吗?", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if(DR == DialogResult.Yes)
                        {
                            ListViewItem it = FieldsListView.SelectedItems[0];
                            BillUiFieldsItem field = (BillUiFieldsItem)it.Tag;
                            SelectGroup.removeNode(field);
                            it.Tag = null;
                            FieldsListView.Items.Remove(it);
                            field = null;
                            it = null;
                            HideFieldProperty();
                            SaveStatus = false;
                        }
                    }
                    else if (FieldsListView.SelectedItems.Count > 1)
                    {
                        DialogResult DR = XtraMessageBox.Show("您确认要删除当前选中字段吗?", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (DR == DialogResult.Yes)
                        {
                            foreach(ListViewItem it in FieldsListView.SelectedItems)
                            {
                                BillUiFieldsItem field = (BillUiFieldsItem)it.Tag;
                                SelectGroup.removeNode(field);
                                it.Tag = null;
                                FieldsListView.Items.Remove(it);
                                field = null;
                            }
                            HideFieldProperty();
                            SaveStatus = false;
                        }
                    }
                }
                else //删除分组
                {
                    if(FieldsListView.SelectedItems.Count == 1)
                    {
                        DialogResult DR = XtraMessageBox.Show("您确认要删除当前选中分组吗?", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (DR == DialogResult.Yes)
                        {
                            ListViewItem it = FieldsListView.SelectedItems[0];
                            BillUiGroupItems field = (BillUiGroupItems)it.Tag;
                            Module.UiField.removeNode(field);
                            it.Tag = null;
                            FieldsListView.Items.Remove(it);
                            field = null;
                            it = null;
                            HideFieldProperty();
                            SaveStatus = false;
                        }
                    }
                }
            }
            else if (FieldTabControl.SelectedTabPage.Name == "EventTabPage")//填充事件
            {
                if (EventListView.SelectedItems.Count == 1)
                {
                    DialogResult DR = XtraMessageBox.Show("您确认要删除当前选中事件吗?", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if(DR == DialogResult.Yes)
                    {
                        ListViewItem it = EventListView.SelectedItems[0];
                        BillDataFillItem field = (BillDataFillItem)it.Tag;
                        Module.DataFill.removeNode(field);
                        it.Tag = null;
                        EventListView.Items.Remove(it);
                        field = null;
                        it = null;
                        HideFieldProperty();
                        SaveStatus = false;
                    }
                }
            }
            else if (FieldTabControl.SelectedTabPage.Name == "ScriptTabPage")//脚本代码
            {
                if(ScriptListView.SelectedItems.Count == 1)
                {
                    DialogResult DR = XtraMessageBox.Show("您确认要删除当前选中脚本吗?", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if(DR == DialogResult.Yes)
                    {
                        ListViewItem it = ScriptListView.SelectedItems[0];
                        BillExpandJsCodeItem js = it.Tag as BillExpandJsCodeItem;
                        Module.ExpandCode.Jscript.removeNode(js);
                        it.Tag = null;
                        ScriptListView.Items.Remove(it);
                        js = null;
                        it = null;
                        HideFieldProperty();
                        SaveStatus = false;
                    }
                    
                }
            }
            else if (FieldTabControl.SelectedTabPage.Name == "JavaTabPage")//扩展代码
            {
                if (JavaListView.SelectedItems.Count == 1)
                {
                    DialogResult DR = XtraMessageBox.Show("您确认要删除当前选中脚本吗?", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if(DR == DialogResult.Yes)
                    {
                        ListViewItem it = JavaListView.SelectedItems[0];
                        BillExpandJavaCodeItem java = it.Tag as BillExpandJavaCodeItem;
                        Module.ExpandCode.JavaPlugin.removeNode(java);
                        it.Tag = null;
                        JavaListView.Items.Remove(it);
                        java = null;
                        it = null;
                        HideFieldProperty();
                        SaveStatus = false;
                    }
                    
                }
            }
        }

        /// <summary>
        /// 复制功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FieldPopCopyNode_Click(object sender, EventArgs e)
        {
            if (FieldTabControl.SelectedTabPage.Name == "FieldTabPage")
            {
                if (SelectGroup != null)//复制字段
                {
                    if (FieldsListView.SelectedItems.Count > 0)
                    {
                        bool sccuess = true;
                        BillUiFieldsItem[] fields = new BillUiFieldsItem[FieldsListView.SelectedItems.Count];
                        int i = 0;
                        foreach(ListViewItem it in FieldsListView.SelectedItems)
                        {
                            try
                            {
                                BillUiFieldsItem Cp = (BillUiFieldsItem)((BillUiFieldsItem)it.Tag).Clone();
                                fields[i] = Cp;
                            }catch(Exception ee)
                            {
                                logger.Error("字段不支持复制", ee);
                                RootPanel.AddConsoleText("字段不支持复制", true);
                                sccuess = false;
                            }
                            i++;
                        }
                        if (sccuess)
                        {
                            JClipboard.copy(fields);
                            RootPanel.AddConsoleText("复制字段Len=="+fields.Length, false);
                        }
                    }
                }
                else //新建分组
                {
                    if (FieldsListView.SelectedItems.Count > 0)
                    {
                        bool sccuess = true;
                        BillUiGroupItems[] groups = new BillUiGroupItems[FieldsListView.SelectedItems.Count];
                        for(int i = 0; i < FieldsListView.SelectedItems.Count; i++)
                        {
                            BillUiGroupItems it = (BillUiGroupItems)FieldsListView.SelectedItems[i].Tag;
                            try
                            {
                                BillUiGroupItems cp = (BillUiGroupItems)it.Clone();
                                groups[i] = cp;
                            }
                            catch(Exception ee)
                            {
                                logger.Error("字段组不支持复制", ee);
                                RootPanel.AddConsoleText("字段组不支持复制", true);
                                sccuess = false;
                            }
                        }
                        if (sccuess)
                        {
                            JClipboard.copy(groups);
                        }
                    }
                }
            }
            else if (FieldTabControl.SelectedTabPage.Name == "EventTabPage")//填充事件
            {
                if (EventListView.SelectedItems.Count > 0)
                {
                    bool sccuess = true;
                    BillDataFillItem[] fills = new BillDataFillItem[EventListView.SelectedItems.Count];
                    for(int i = 0; i < EventListView.SelectedItems.Count; i++)
                    {
                        BillDataFillItem fill = EventListView.SelectedItems[i].Tag as BillDataFillItem;
                        try
                        {
                            fills[i] = fill.Clone() as BillDataFillItem;
                        }catch(Exception ee)
                        {
                            logger.Error("填充事件不支持复制", ee);
                            RootPanel.AddConsoleText("填充事件不支持复制", true);
                            sccuess = false;
                            break;
                        }
                    }
                    if (sccuess)
                    {
                        JClipboard.copy(fills);
                    }
                }
            }
            else if (FieldTabControl.SelectedTabPage.Name == "ScriptTabPage")//脚本代码
            {
                if (ScriptListView.SelectedItems.Count > 0)
                {
                    bool sccuess = true;
                    BillExpandJsCodeItem[] fills = new BillExpandJsCodeItem[ScriptListView.SelectedItems.Count];
                    for (int i = 0; i < ScriptListView.SelectedItems.Count; i++)
                    {
                        BillExpandJsCodeItem fill = ScriptListView.SelectedItems[i].Tag as BillExpandJsCodeItem;
                        try
                        {
                            fills[i] = fill.Clone() as BillExpandJsCodeItem;
                        }
                        catch (Exception ee)
                        {
                            logger.Error("脚本不支持复制", ee);
                            RootPanel.AddConsoleText("脚本不支持复制", true);
                            sccuess = false;
                            break;
                        }
                    }
                    if (sccuess)
                    {
                        JClipboard.copy(fills);
                    }
                }
            }
            else if (FieldTabControl.SelectedTabPage.Name == "JavaTabPage")//扩展代码
            {
                if (JavaListView.SelectedItems.Count > 0)
                {
                    bool sccuess = true;
                    BillExpandJavaCodeItem[] fills = new BillExpandJavaCodeItem[JavaListView.SelectedItems.Count];
                    for (int i = 0; i < JavaListView.SelectedItems.Count; i++)
                    {
                        BillExpandJavaCodeItem fill = JavaListView.SelectedItems[i].Tag as BillExpandJavaCodeItem;
                        try
                        {
                            fills[i] = fill.Clone() as BillExpandJavaCodeItem;
                        }
                        catch (Exception ee)
                        {
                            logger.Error("脚本不支持复制", ee);
                            RootPanel.AddConsoleText("脚本不支持复制", true);
                            sccuess = false;
                            break;
                        }
                    }
                    if (sccuess)
                    {
                        JClipboard.copy(fills);
                    }
                }
            }
        }
        /// <summary>
        /// 粘贴功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FieldPopPasteNode_Click(object sender, EventArgs e)
        {
            if (readOnly) return;
            if (!JClipboard.ContainData()) return;
            if (FieldTabControl.SelectedTabPage.Name == "FieldTabPage")
            {
                if (SelectGroup != null)//粘贴字段
                {
                    if(JClipboard.Paste()[0] is BillUiFieldsItem)
                    {
                        foreach(BillUiFieldsItem field in JClipboard.Paste())
                        {
                            SelectGroup.addNode(field, Module);
                            ListViewItem it = new ListViewItem(field.Caption, 1);
                            it.Tag = field;
                            FieldsListView.Items.Add(it);
                        }
                        SaveStatus = false;
                        JClipboard.Clear();
                    }
                }
                else //新建分组
                {
                    if(JClipboard.Paste()[0] is BillUiGroupItems)
                    {
                        foreach (BillUiGroupItems group in JClipboard.Paste())
                        {
                            Module.UiField.addNode(group, Module);
                            ListViewItem it = new ListViewItem(group.Caption, 0);
                            it.Tag = group;
                            FieldsListView.Items.Add(it);
                        }
                        SaveStatus = false;
                        JClipboard.Clear();
                    }
                }
            }
            else if (FieldTabControl.SelectedTabPage.Name == "EventTabPage")//填充事件
            {
                if(JClipboard.Paste()[0] is BillDataFillItem)
                {
                    foreach(BillDataFillItem fill in JClipboard.Paste())
                    {
                        Module.DataFill.addNode(fill,Module);
                        ListViewItem it = new ListViewItem(fill.Caption, 5);
                        it.Tag = fill;
                        EventListView.Items.Add(it);
                    }
                    SaveStatus = false;
                    JClipboard.Clear();
                }
            }
            else if (FieldTabControl.SelectedTabPage.Name == "ScriptTabPage")//脚本代码
            {
                if (JClipboard.Paste()[0] is BillExpandJsCodeItem)
                {
                    foreach (BillExpandJsCodeItem fill in JClipboard.Paste())
                    {
                        //Module.DataFill.addNode(fill, Module);
                        Module.ExpandCode.Jscript.addNode(fill, Module);
                        ListViewItem it = new ListViewItem(fill.Caption, 2);
                        it.Tag = fill;
                        ScriptListView.Items.Add(it);
                    }
                    SaveStatus = false;
                    JClipboard.Clear();
                }
                
            }
            else if (FieldTabControl.SelectedTabPage.Name == "JavaTabPage")//扩展代码
            {
                if (JClipboard.Paste()[0] is BillExpandJavaCodeItem)
                {
                    foreach (BillExpandJavaCodeItem fill in JClipboard.Paste())
                    {
                        //Module.DataFill.addNode(fill, Module);
                        Module.ExpandCode.JavaPlugin.addNode(fill, Module);
                        ListViewItem it = new ListViewItem(fill.Caption, 2);
                        it.Tag = fill;
                        JavaListView.Items.Add(it);
                    }
                    SaveStatus = false;
                    JClipboard.Clear();
                }
            }
        }
        /// <summary>
        /// 剪切功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FieldPopCutNode_Click(object sender, EventArgs e)
        {
            if (readOnly) return;
            if (FieldTabControl.SelectedTabPage.Name == "FieldTabPage")
            {
                if (SelectGroup != null)//复制字段
                {
                    if (FieldsListView.SelectedItems.Count > 0)
                    {
                        moveing = true;
                        bool sccuess = true;
                        BillUiFieldsItem[] fields = new BillUiFieldsItem[FieldsListView.SelectedItems.Count];
                        int i = 0;
                        foreach (ListViewItem it in FieldsListView.SelectedItems)
                        {
                            try
                            {
                                BillUiFieldsItem Cp = (BillUiFieldsItem)((BillUiFieldsItem)it.Tag).Clone();
                                fields[i] = Cp;
                            }
                            catch (Exception ee)
                            {
                                logger.Error("字段不支持复制", ee);
                                RootPanel.AddConsoleText("字段不支持复制", true);
                                sccuess = false;
                            }
                            i++;
                        }
                        if (sccuess)
                        {
                            JClipboard.copy(fields);
                            foreach (ListViewItem it in FieldsListView.SelectedItems)
                            {
                                BillUiFieldsItem field = it.Tag as BillUiFieldsItem;
                                SelectGroup.removeNode(field);
                                it.Tag = null;
                                field = null;
                                FieldsListView.Items.Remove(it);
                            }
                            SaveStatus = false;
                            HideFieldProperty();
                            
                        }
                        moveing = false;
                    }
                }
                else //新建分组
                {
                    if (FieldsListView.SelectedItems.Count > 0)
                    {
                        bool sccuess = true;
                        BillUiGroupItems[] groups = new BillUiGroupItems[FieldsListView.SelectedItems.Count];
                        for (int i = 0; i < FieldsListView.SelectedItems.Count; i++)
                        {
                            BillUiGroupItems it = (BillUiGroupItems)FieldsListView.SelectedItems[i].Tag;
                            try
                            {
                                BillUiGroupItems cp = (BillUiGroupItems)it.Clone();
                                groups[i] = cp;
                            }
                            catch (Exception ee)
                            {
                                logger.Error("字段组不支持复制", ee);
                                RootPanel.AddConsoleText("字段组不支持复制", true);
                                sccuess = false;
                            }
                        }
                        if (sccuess)
                        {
                            JClipboard.copy(groups);
                            foreach (ListViewItem it in FieldsListView.SelectedItems)
                            {
                                BillUiGroupItems field = it.Tag as BillUiGroupItems;
                                Module.UiField.removeNode(field);
                                it.Tag = null;
                                field = null;
                                FieldsListView.Items.Remove(it);
                            }
                            SaveStatus = false;
                        }
                    }
                }
            }
            else if (FieldTabControl.SelectedTabPage.Name == "EventTabPage")//填充事件
            {
                if (EventListView.SelectedItems.Count > 0)
                {
                    bool sccuess = true;
                    BillDataFillItem[] fills = new BillDataFillItem[EventListView.SelectedItems.Count];
                    for (int i = 0; i < EventListView.SelectedItems.Count; i++)
                    {
                        BillDataFillItem fill = EventListView.SelectedItems[i].Tag as BillDataFillItem;
                        try
                        {
                            fills[i] = fill.Clone() as BillDataFillItem;
                        }
                        catch (Exception ee)
                        {
                            logger.Error("填充事件不支持复制", ee);
                            RootPanel.AddConsoleText("填充事件不支持复制", true);
                            sccuess = false;
                            break;
                        }
                    }
                    if (sccuess)
                    {
                        JClipboard.copy(fills);
                        for (int i = 0; i < EventListView.SelectedItems.Count; i++)
                        {
                            ListViewItem view = EventListView.SelectedItems[i];
                            BillDataFillItem fill = view.Tag as BillDataFillItem;
                            Module.DataFill.removeNode(fill);
                            view.Tag = null;
                            fill = null;
                            EventListView.Items.Remove(view);
                        }
                        SaveStatus = false;
                    }
                }
            }
            else if (FieldTabControl.SelectedTabPage.Name == "ScriptTabPage")//脚本代码
            {
                if (ScriptListView.SelectedItems.Count > 0)
                {
                    bool sccuess = true;
                    BillExpandJsCodeItem[] fills = new BillExpandJsCodeItem[ScriptListView.SelectedItems.Count];
                    for (int i = 0; i < ScriptListView.SelectedItems.Count; i++)
                    {
                        BillExpandJsCodeItem fill = ScriptListView.SelectedItems[i].Tag as BillExpandJsCodeItem;
                        try
                        {
                            fills[i] = fill.Clone() as BillExpandJsCodeItem;
                        }
                        catch (Exception ee)
                        {
                            logger.Error("脚本不支持复制", ee);
                            RootPanel.AddConsoleText("脚本不支持复制", true);
                            sccuess = false;
                            break;
                        }
                    }
                    if (sccuess)
                    {
                        JClipboard.copy(fills);
                        for (int i = 0; i < ScriptListView.SelectedItems.Count; i++)
                        {
                            ListViewItem view = ScriptListView.SelectedItems[i];
                            BillExpandJsCodeItem fill = view.Tag as BillExpandJsCodeItem;
                            Module.ExpandCode.Jscript.removeNode(fill);
                            view.Tag = null;
                            fill = null;
                            ScriptListView.Items.Remove(view);
                        }
                        SaveStatus = false;
                    }
                }
            }
            else if (FieldTabControl.SelectedTabPage.Name == "JavaTabPage")//扩展代码
            {
                if (JavaListView.SelectedItems.Count > 0)
                {
                    bool sccuess = true;
                    BillExpandJavaCodeItem[] fills = new BillExpandJavaCodeItem[JavaListView.SelectedItems.Count];
                    for (int i = 0; i < JavaListView.SelectedItems.Count; i++)
                    {
                        BillExpandJavaCodeItem fill = JavaListView.SelectedItems[i].Tag as BillExpandJavaCodeItem;
                        try
                        {
                            fills[i] = fill.Clone() as BillExpandJavaCodeItem;
                        }
                        catch (Exception ee)
                        {
                            logger.Error("脚本不支持复制", ee);
                            RootPanel.AddConsoleText("脚本不支持复制", true);
                            sccuess = false;
                            break;
                        }
                    }
                    if (sccuess)
                    {
                        JClipboard.copy(fills);
                        for (int i = 0; i < JavaListView.SelectedItems.Count; i++)
                        {
                            ListViewItem view = JavaListView.SelectedItems[i];
                            BillExpandJavaCodeItem fill = view.Tag as BillExpandJavaCodeItem;
                            Module.ExpandCode.JavaPlugin.removeNode(fill);
                            view.Tag = null;
                            fill = null;
                            JavaListView.Items.Remove(view);
                        }
                        SaveStatus = false;
                    }
                }
            }
        }


        private void FieldPopUpNode_Click(object sender, EventArgs e)
        {
            if (readOnly) return;
            moveing = true;
            if (FieldsListView.SelectedItems.Count != 1) return;
            if(FieldTabControl.SelectedTabPage.Name == "FieldTabPage")
            {
                if (SelectGroup != null)//字段上移
                {
                    ListViewItem view = FieldsListView.SelectedItems[0];
                    BillUiFieldsItem field = view.Tag as BillUiFieldsItem;
                    int index = FieldsListView.Items.IndexOf(view);
                    if (index > 0)
                    {
                        SelectGroup.moveUp(field);
                        ListViewItem PevView = FieldsListView.Items[index - 1];
                        PevView.Tag = SelectGroup.Fields[index - 1];
                        PevView.Text = (PevView.Tag as BillUiFieldsItem).Caption;
                        view.Tag = SelectGroup.Fields[index];
                        view.Text = (view.Tag as BillUiFieldsItem).Caption;
                        PevView.Selected = true;
                        view.Selected = false;
                        SaveStatus = false;
                    }
                }
                else//字段组上组
                {
                    ListViewItem view = FieldsListView.SelectedItems[0];
                    BillUiGroupItems group = view.Tag as BillUiGroupItems;
                    int index = FieldsListView.Items.IndexOf(view);
                    if(index > 0)
                    {
                        Module.UiField.moveUp(group);
                        ListViewItem PevView = FieldsListView.Items[index - 1];
                        PevView.Tag = Module.UiField.FieldsProperties[index - 1];
                        PevView.Text = (PevView.Tag as BillUiGroupItems).Caption;
                        view.Tag = Module.UiField.FieldsProperties[index];
                        view.Text = (view.Tag as BillUiGroupItems).Caption;
                        PevView.Selected = true;
                        view.Selected = false;
                        SaveStatus = false;
                    }
                }

            }
            moveing = false;
        }

        private void FieldPopDownNode_Click(object sender, EventArgs e)
        {
            if (readOnly) return;
            moveing = true;
            if (FieldTabControl.SelectedTabPage.Name == "FieldTabPage")
            {
                if (SelectGroup != null)//字段上移
                {
                    ListViewItem view = FieldsListView.SelectedItems[0];
                    BillUiFieldsItem field = view.Tag as BillUiFieldsItem;
                    int index = FieldsListView.Items.IndexOf(view);
                    if (index < FieldsListView.Items.Count - 1)
                    {
                        SelectGroup.moveDown(field);
                        ListViewItem LastView = FieldsListView.Items[index + 1];
                        LastView.Tag = SelectGroup.Fields[index + 1];
                        LastView.Text = (LastView.Tag as BillUiFieldsItem).Caption;
                        view.Tag = SelectGroup.Fields[index];
                        view.Text = (view.Tag as BillUiFieldsItem).Caption;
                        LastView.Selected = true;
                        view.Selected = false;
                        SaveStatus = false;
                    }
                }
                else//字段组上组
                {
                    ListViewItem view = FieldsListView.SelectedItems[0];
                    BillUiGroupItems group = view.Tag as BillUiGroupItems;
                    int index = FieldsListView.Items.IndexOf(view);
                    if (index < FieldsListView.Items.Count -1)
                    {
                        Module.UiField.moveDown(group);
                        ListViewItem LastView = FieldsListView.Items[index + 1];
                        LastView.Tag = Module.UiField.FieldsProperties[index + 1];
                        LastView.Text = (LastView.Tag as BillUiGroupItems).Caption;
                        view.Tag = Module.UiField.FieldsProperties[index];
                        view.Text = (view.Tag as BillUiGroupItems).Caption;
                        LastView.Selected = true;
                        view.Selected = false;
                        SaveStatus = false;
                    }
                }
            }
            moveing = false;
        }


        private void FieldPopCheckAllNode_Click(object sender, EventArgs e)
        {
            moveing = true;
            HideFieldProperty();
            ListView View = (ListView)FieldTabControl.SelectedTabPage.Controls[0];
            if (View.Items.Count > 0)
            {
                for (int i = 0; i < View.Items.Count; i++)
                {
                    ListViewItem it = View.Items[i];
                    it.Selected = true;
                }
            }
            moveing = false;
        }


        #endregion



        private void ModulePropertyList_CellValueChanged(object sender, DevExpress.XtraVerticalGrid.Events.CellValueChangedEventArgs e)
        {
            if(e.Row.Properties.FieldName == "Caption")//修改模块标签
            {
                ReLoadMenu = true;
            }
            SaveStatus = false;
        }

       


        private void ScriptListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (moveing) return;
            if (ScriptListView.SelectedItems.Count > 0)
            {
                PropertyTabControl.SelectedTabPageIndex = 0;
                if (JScriptPropery.Visible == false)
                {
                    HideFieldProperty();
                    JScriptPropery.Dock = DockStyle.Fill;
                    JScriptPropery.Visible = true;
                }
                int len = ScriptListView.SelectedItems.Count;
                if (len == 1)
                {
                    object temp = ScriptListView.SelectedItems[0].Tag;
                    if (temp is BillExpandJsCodeItem)
                    {
                        BillExpandJsCodeItem code = (BillExpandJsCodeItem)temp;
                        JScriptPropery.SelectField = new BillUiJscriptToProperty(code);
                    }
                }
                else
                {
                    BillUiJscriptToProperty[] JScriptList = new BillUiJscriptToProperty[len];
                    for (int i = 0; i < len; i++)
                    {
                        BillExpandJsCodeItem code = (BillExpandJsCodeItem)ScriptListView.SelectedItems[i].Tag;
                        JScriptList[i] = new BillUiJscriptToProperty(code);
                    }
                    JScriptPropery.SelectFields = JScriptList;
                }
            }
        }


        private void FieldPopMenu_Opened(object sender, EventArgs e)
        {
            if (FieldPopMenu.SourceControl.Name == "FieldsListView")
            {
                FieldPopUpNode.Enabled = true;
                FieldPopDownNode.Enabled = true;
            }
            else
            {
                FieldPopUpNode.Enabled = false;
                FieldPopDownNode.Enabled = false;
            }
            if (JClipboard.ContainData())
            {
                FieldPopPasteNode.Enabled = true;
            }
            else
            {
                FieldPopPasteNode.Enabled = false;
            }
        }
        /// <summary>
        /// 字段Caption改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FieldCaptionChanged(object sender,PropertyDataEvent e)
        {
            if (FieldsListView.SelectedItems.Count == 1 && SelectGroup != null)
            {
                ListViewItem item = FieldsListView.SelectedItems[0];
                item.Text = (string)e.Value;
            }
        }

        private void LinkerChanged(object sender, PropertyDataEvent e)
        {
            ShowJScript();
        }
        /// <summary>
        /// 字段组改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GroupCaptionChanged(object sender, PropertyDataEvent e)
        {
            if(FieldsListView.SelectedItems.Count==1 && FieldsListView.SelectedItems[0].Tag is BillUiGroupItems)
            {
                ListViewItem item = FieldsListView.SelectedItems[0];
                item.Text = (string)e.Value;
            }
        }
        /// <summary>
        /// 事件名称改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EventCaptionChanged(object sender,PropertyDataEvent e)
        {
            if (EventListView.SelectedItems.Count == 1)
            {
                ListViewItem item = EventListView.SelectedItems[0];
                item.Text = (string)e.Value;
            }
        }
        /// <summary>
        /// 脚本名称改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void JsCaptionChanged(object sender,PropertyDataEvent e)
        {
            if (ScriptListView.SelectedItems.Count == 1)
            {
                ListViewItem item = ScriptListView.SelectedItems[0];
                item.Text = (string)e.Value;
            }
        }
        /// <summary>
        /// java扩展代码名称改变
        /// </summary>
        /// <param name="sneder"></param>
        /// <param name="e"></param>
        private void JavaCaptionChanged(object sneder,PropertyDataEvent e)
        {
            if (JavaListView.SelectedItems.Count == 1)
            {
                ListViewItem item = JavaListView.SelectedItems[0];
                item.Text = (string)e.Value;
            }
        }
        /// <summary>
        /// 数据源设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RepositoryDataSourceEdit_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            if (QueryForm == null) QueryForm = new ModuleQueryUiForm(Module, RootPanel, this);
            QueryForm.ReLoadList();
            QueryForm.ShowDialog(FindForm());
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RepositoryItemIconEdit_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            if (IconForm == null) IconForm = new ModuleIconSelectForm(RootPanel, this, Module);
            if(IconForm.ShowDialog(RootPanel.RootForm) == DialogResult.OK)
            {
                ModulePropertyList.Refresh();
            }
        }
        /// <summary>
        /// 上传附件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RepositoryAttchEdit_ButtonClick(object sender, ButtonPressedEventArgs e)
        {

        }
        /// <summary>
        /// 字段界面点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FieldsListView_Click(object sender, EventArgs e)
        {   
            //调用changed
            FieldsListView_SelectedIndexChanged(sender, e);
        }
        /// <summary>
        /// 事件填充点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EventListView_Click(object sender, EventArgs e)
        {
            EventListView_SelectedIndexChanged(sender, e);
        }
        /// <summary>
        /// javascript点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ScriptListView_Click(object sender, EventArgs e)
        {
            ScriptListView_SelectedIndexChanged(sender, e);
        }

        /// <summary>
        /// java点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void JavaListView_Click(object sender, EventArgs e)
        {
            JavaLiistView_SelectedIndexChanged(sender, e);
        }

        #endregion


    }
}
